package com.junyu.common;

import lombok.Data;

/**
 * 响应结果实体类
 */
@Data
public class Result<T> {
    private Integer code;
    private String message;
    private T data;

    /**
     * @param code    响应码
     * @param message 响应信息内容
     * @param data    返回数据
     */
    public static <T> Result setResult(Integer code, String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    /**
     *
     * @param resultCodeEnum
     * @return
     * @param <T>
     */
    public static <T> Result setResult(ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(null);
        return result;
    }

    /**
     * 调用成功无参
     */
    public static <T> Result ok() {
        return setResult(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), null);
    }


    /**
     * 调用成功含参
     */
    public static <T> Result ok(T data) {
        return setResult(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 调用失败
     */
    public static <T> Result fail() {
        return setResult(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getMessage(), null);
    }


    /**
     * 链式调用
     */
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    /**
     * 链式调用
     */
    public Result message(String message) {
        this.setMessage(message);
        return this;
    }
}